//
//  UIViewController+SJHint.swift
//  WuYeUser
//
//  Created by Saxon on 2018/3/21.
//  Copyright © 2018年 com.WuyeUser.remotevillage. All rights reserved.
//

import Foundation
import MBProgressHUD

struct SJHintKey {
    static let hudKey = UnsafeRawPointer.init(bitPattern: "controllerHudKey".hashValue)
}

public extension UIViewController {
    private var hud: MBProgressHUD? {
        get {
            return objc_getAssociatedObject(self, SJHintKey.hudKey!) as? MBProgressHUD
        } set {
            objc_setAssociatedObject(self, SJHintKey.hudKey!, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    //纯文本提示
    public func showHint(_ hint: String, view:UIView = UIApplication.shared.keyWindow!, offset:CGFloat = 0){
        if hint == "" || hint.isEmpty {
            return
        }
        hideHud()
        
        DispatchQueue.main.async {
            self.hud = MBProgressHUD.showAdded(to: view, animated: true)
            if let h = self.hud {
                h.mode = .text
                h.bezelView.style = .solidColor
                h.bezelView.color = UIColor.black.withAlphaComponent(0.8)
                h.contentColor = UIColor.white
                h.label.text = hint
                h.offset.y = offset
                h.removeFromSuperViewOnHide = true
                h.isUserInteractionEnabled = false
                h.hide(animated: true, afterDelay: 1.5)
            }
        }
        
        
    }
    public func hideHud() {
        if let h = hud {
            DispatchQueue.main.async {
                h.hide(animated: true)
                self.hud = nil
            }
        }
        
    }
}
